package justdoit.spring;

import justdoit.spring.dao.PaymentDAO;
import justdoit.spring.domain.Payment;
import justdoit.spring.service.PaymentService;
import java.math.BigDecimal;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/test-context.xml" })
public class PaymentServiceTest {

	@Resource(name = "paymentDAO")
	private PaymentDAO dao;

	@Resource(name = "paymentService")
	private PaymentService service;

	@Test
	public void testPayments() {
		BigDecimal amount = new BigDecimal("100");
		long payerId = 1L;
		long recipientId = 2L;
		// prepare data for test
		final Payment expectedPayment = new Payment(amount, payerId,
				recipientId);

		// prepare mock
		reset(dao);
		dao.savePayment(expectedPayment);
		final long newId = 1L;
		expectLastCall();
		expectedPayment.setId(newId);
//		.callback(new Runnable() {
//
//			public void run() {
//				expectedPayment.setId(newId);
//			}
//		});
		
		replay(dao);

		service.addPayment(expectedPayment);

		assertEquals(newId, expectedPayment.getId());
		verify(dao);
	}
}